<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="影视名称" prop="movieTitle">
        <el-input
          v-model="queryParams.movieTitle"
          placeholder="请输入影视名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分范围">
        <el-select v-model="queryParams.rateRange" placeholder="请选择评分范围" clearable>
          <el-option label="1-2分" value="1-2" />
          <el-option label="3-4分" value="3-4" />
          <el-option label="5-6分" value="5-6" />
          <el-option label="7-8分" value="7-8" />
          <el-option label="9-10分" value="9-10" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['movie:rate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['movie:rate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评分ID" align="center" prop="rateId" width="80" />
      <el-table-column label="影视海报" align="center" prop="moviePoster" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.moviePoster"
            :preview-src-list="[scope.row.moviePoster]"
            fit="cover"
            v-if="scope.row.moviePoster"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><VideoPlay /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="影视名称" align="center" prop="movieTitle" :show-overflow-tooltip="true" />
      <el-table-column label="会员信息" align="center" width="150">
        <template #default="scope">
          <div style="display: flex; align-items: center; justify-content: center;">
            <el-avatar :size="30" :src="scope.row.memberAvatar || ''" style="margin-right: 8px;">
              <el-icon><UserFilled /></el-icon>
            </el-avatar>
            <span>{{ scope.row.memberNickname || `会员${scope.row.memberId}` }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="rate" width="200">
        <template #default="scope">
          <div>
            <el-rate
              v-model="scope.row.rate"
              disabled
              :max="5"
              :allow-half="true"
              show-score
              :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            />
            <div style="margin-top: 5px; color: #ff9900; font-weight: bold;">
              {{ scope.row.rate * 2 }} 分
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评价内容" align="center" prop="comment" :show-overflow-tooltip="true" />
      <el-table-column label="评分时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['movie:rate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 评分统计对话框 -->
    <el-dialog title="影视评分统计" v-model="statsOpen" width="600px" append-to-body>
      <div v-if="movieStats">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="影视名称">{{ movieStats.movieTitle }}</el-descriptions-item>
          <el-descriptions-item label="平均评分">
            <el-rate
              v-model="movieStats.avgRate"
              disabled
              :max="5"
              :allow-half="true"
              show-score
            />
          </el-descriptions-item>
          <el-descriptions-item label="评分人数">{{ movieStats.rateCount }} 人</el-descriptions-item>
          <el-descriptions-item label="最高评分">{{ movieStats.maxRate * 2 }} 分</el-descriptions-item>
          <el-descriptions-item label="最低评分">{{ movieStats.minRate * 2 }} 分</el-descriptions-item>
          <el-descriptions-item label="评分分布" :span="2">
            <div style="width: 100%;">
              <div v-for="i in 5" :key="i" style="display: flex; align-items: center; margin-bottom: 10px;">
                <span style="width: 40px;">{{ i }}星</span>
                <el-progress
                  :percentage="getStarPercentage(i)"
                  :stroke-width="20"
                  style="flex: 1; margin: 0 10px;"
                />
                <span style="width: 60px;">{{ getStarCount(i) }}人</span>
              </div>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Rate">
import { listRate, delRate, getMovieRate } from "@/api/movie/rate";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { UserFilled, VideoPlay } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const rateList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const statsOpen = ref(false);
const movieStats = ref(null);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    movieTitle: null,
    memberId: null,
    rateRange: null
  }
});

const { queryParams } = toRefs(data);

/** 查询评分列表 */
function getList() {
  loading.value = true;
  listRate(queryParams.value).then(response => {
    rateList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.rateId);
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const rateIds = row.rateId || ids.value;
  proxy.$modal.confirm('是否确认删除评分编号为"' + rateIds + '"的数据项？').then(function() {
    return delRate(rateIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('movie/rate/export', {
    ...queryParams.value
  }, `movie_rate_${new Date().getTime()}.xlsx`)
}

/** 查看统计 */
function handleStats(movieId) {
  getMovieRate(movieId).then(response => {
    movieStats.value = response.data;
    statsOpen.value = true;
  });
}

// 获取星级百分比
function getStarPercentage(star) {
  if (!movieStats.value || !movieStats.value.distribution) return 0;
  const total = movieStats.value.rateCount || 1;
  const count = movieStats.value.distribution[star] || 0;
  return Math.round((count / total) * 100);
}

// 获取星级人数
function getStarCount(star) {
  if (!movieStats.value || !movieStats.value.distribution) return 0;
  return movieStats.value.distribution[star] || 0;
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}
</style>